//
//  PeriodTimeView.m
//  Demo
//
//  Created by MacPro on 15/12/9.
//  Copyright © 2015年 pccb. All rights reserved.
//

#import "PeriodTimeView.h"

#define FirstComponent 0
#define SubComponent 1

@interface PeriodTimeView ()<UIPickerViewDelegate,UIPickerViewDataSource>

@property (strong, nonatomic) UIPickerView *timePickerView;

@property (nonatomic, retain) NSArray *pickerArray;
@property (nonatomic, retain) NSArray *subPickerArray;
@property (nonatomic, retain) NSArray *allPickerArray;
@property (strong, nonatomic) UILabel *textLabel;

@property (copy, nonatomic) NSString *startTimeStr;
@property (copy, nonatomic) NSString *endTimeStr;

@end

@implementation PeriodTimeView

- (instancetype)initPeriodTimeViewWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        
        UIView *containerView = [[UIView alloc] init];
        containerView.backgroundColor = [UIColor whiteColor];
        containerView.frame = CGRectMake(15.0f, 0, self.width - 30.0f, self.height);
        containerView.layer.borderWidth = 1.0f;
        containerView.layer.borderColor = DefaultColor_NavigationBar.CGColor;
        [self addSubview:containerView];
        
        UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, containerView.width, 30.0f)];
        topView.backgroundColor = DefaultColor_NavigationBar;
        [containerView addSubview:topView];
        
        CGFloat buttonWidth = containerView.width /6.0f;
        
        for (int i = 1; i < 3; i++) {
            UIButton * submitBtn          = [UIButton buttonWithType:UIButtonTypeCustom];
            submitBtn.frame = CGRectMake(topView.width - i * (buttonWidth + 10.0f), 5.0f, buttonWidth, 20.f);
            
            [submitBtn setTitleColor:DefaultColor_NavigationBar forState:UIControlStateNormal];
            [submitBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
            submitBtn.backgroundColor = [UIColor whiteColor];
            submitBtn.titleLabel.font = Default_Font_14;
            [topView addSubview:submitBtn];
            
            if (i == 1) {
                [submitBtn setTitle:@"确认" forState:UIControlStateNormal];
                [submitBtn addTarget:self action:@selector(clickSubmitBtnAction:) forControlEvents:UIControlEventTouchUpInside];
            }else {
                [submitBtn setTitle:@"取消" forState:UIControlStateNormal];
                [submitBtn addTarget:self action:@selector(clickCancelBtnAction:) forControlEvents:UIControlEventTouchUpInside];
            }
            
            _pickerArray = @[@"8:00",@"9:00",@"10:00",@"11:00",@"12:00",@"13:00",@"14:00",@"15:00",@"16:00",@"17:00",@"18:00",@"19:00"];
            _subPickerArray = @[@"9:00",@"10:00",@"11:00",@"12:00",@"13:00",@"14:00",@"15:00",@"16:00",@"17:00",@"18:00",@"19:00",@"20:00"];
            _allPickerArray = @[@"8:00",@"9:00",@"10:00",@"11:00",@"12:00",@"13:00",@"14:00",@"15:00",@"16:00",@"17:00",@"18:00",@"19:00",@"20:00"];
            
            _startTimeStr = _pickerArray[0];
            _endTimeStr = _subPickerArray[0];
            
            _timePickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(10.0f, topView.bottom + 5.0f, containerView.width - 20.0f, containerView.height - topView.bottom + 10.0f)];//162
            _timePickerView.dataSource = self;
            _timePickerView.delegate = self;
            _timePickerView.backgroundColor = [UIColor whiteColor];
            [containerView addSubview:_timePickerView];
            
            [_timePickerView reloadAllComponents];//刷新UIPickerView
        }
        
    }

    return self;
}

#pragma mark - UIPickerViewDelegate,UIPickerViewDataSource

//返回有几列
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 3;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component==FirstComponent) {
        return [self.pickerArray count];
    }
    if (component==SubComponent) {
        return [self.subPickerArray count];
    }
    
    return 0;
}

//返回指定列，行的高度，就是自定义行的高度
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    
    return 25.0f;
    
}

//返回指定列的宽度
- (CGFloat) pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{

    return  (SCREEN_WIDTH - 30.0f) /2.0f;
}

// 自定义指定列的每行的视图，即指定列的每行的视图行为一致
#pragma mark--UIPickerViewDelegate
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component==FirstComponent) {
        return [self.pickerArray objectAtIndex:row];
    }
    
    if (component==SubComponent) {
        return [self.subPickerArray objectAtIndex:row];
    }
    
    return nil;
}

//显示的标题字体、颜色等属性
- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component{
    
    NSString *string = @"";
    if (component==FirstComponent) {
        string = [self.pickerArray objectAtIndex:row];
    }
    
    if (component==SubComponent) {
        string = [self.subPickerArray objectAtIndex:row];
    }
    
    NSMutableAttributedString *AttributedString = [[NSMutableAttributedString alloc]initWithString:string];
    [AttributedString addAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:14], NSForegroundColorAttributeName:HexRGB(0x454545)} range:NSMakeRange(0, [AttributedString  length])];
    
    return AttributedString;
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
//    NSLog(@"row is %ld,Component is %ld",(long)row,(long)component);
    
    if (component == 0) {
        
        NSInteger count = [self.allPickerArray count];
        
        NSMutableArray *array = [NSMutableArray arrayWithCapacity:1];
        for (NSInteger i = row + 1; i < count; i++) {
            
            [array addObject:[self.allPickerArray objectAtIndex:i]];
            
        }
        
        self.subPickerArray = [NSArray arrayWithArray:array];
        
        [pickerView selectedRowInComponent:1];
        [pickerView reloadComponent:1];
        [pickerView selectRow:0 inComponent:1 animated:YES];
        
        _startTimeStr = _pickerArray[row];
        _endTimeStr = _subPickerArray[0];
        
    }else if (component == 1) {
        
        _endTimeStr = _subPickerArray[row];
        
    }
}

- (void)clickCancelBtnAction:(UIButton *)sender
{
    [self removeFromSuperview];
}

- (void)clickSubmitBtnAction:(UIButton *)sender
{
    if (self.SubmitPeriodTimeBlock) {
        
        self.SubmitPeriodTimeBlock(_startTimeStr,_endTimeStr);
        
    }
    
    //取消弹出框
    [self clickCancelBtnAction:nil];
}

@end
